﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Gui.AspNet2;
using Swaf.Security;

[assembly: WebResource("ScsAspNet.Syrinx.Gui.AspNet.script.ImagePan.js", "application/x-javascript")]
namespace Syrinx.Gui.AspNet
{
	public class ImagePanner : System.Web.UI.UserControl
	{
		protected Unit m_width, m_height;
		protected string m_cssClass;
		protected bool m_bounceMode = false;
		protected string m_initImg;
		protected int m_moveSpeed = 0;
		protected PanControlOption m_panControls = PanControlOption.None;

		public Unit Width { get { return m_width; } set { m_width = value; } }

		public Unit Height { get { return m_height; } set { m_height = value; } }

		public string CssClass { get { return m_cssClass; } set { m_cssClass = value; } }

		public bool Bounce { get { return m_bounceMode; } set { m_bounceMode = value; } }

		public int MoveSpeed { get { return m_moveSpeed; } set { m_moveSpeed = value; } }

		public string ImageUrl { get { return m_initImg; } set { m_initImg = value; } }

		public PanControlOption ControllerOption { get { return m_panControls; } set { m_panControls = value; } }

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			string scriptUrl = Page.ClientScript.GetWebResourceUrl(typeof(ImagePanner), "ScsAspNet.Syrinx.Gui.AspNet.script.ImagePan.js");
			Page.ClientScript.RegisterClientScriptInclude("Swaf.ImagePannerScript", scriptUrl);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			StringBuilder buff = new StringBuilder();
			if (Width.Value != 0)
				buff.Append("width:").Append(Width.ToString()).Append(";");
			if (Height.Value != 0)
				buff.Append("height:").Append(Height.ToString()).Append(";");
			if (!Bounce)
				buff.Append("cursor:pointer;");
			        
			writer.Write(string.Format("<div id='{0}' style='{1}' {2}>", ClientID, buff.ToString(), CssClass==null?"":CssClass));
			if (ControllerOption != PanControlOption.None)
			{
				if (ControllerOption != PanControlOption.None)
				{
					ImagePannerControls ctl = new ImagePannerControls();
					ctl.PannerId = ID;
					if (ControllerOption == PanControlOption.Bounce)
						ctl.UseBounceControls = true;
					Controls.Add(ctl);
				}
			}
			foreach (Control ctl in Controls)
				ctl.RenderControl(writer);
			if (Controls.Count == 0)
				writer.Write("&nbsp;"); //This is needed to keep firefox from allow mouse drags to move the background image!

			writer.Write("</div>");
			bool bounce = (Bounce || ControllerOption == PanControlOption.Bounce);
			writer.Write(string.Format("<script>$(document).ready(function() {{new ImagePan($('#{0}').get(0),{1}, '{2}', {3});}});</script>",
				ClientID, bounce.ToString().ToLower(), ImageUrl == null ? "" : ImageUrl, MoveSpeed));
		}
	}
	public enum PanControlOption { None, Slide, Bounce }
}
